package com.A21MobileSales;
 
import java.util.Calendar;
 
import com.A21.Object.Global;
import com.A21MobileSales.R;
import com.A21MobileSales.DataBase.DatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View; 
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class GlobalDiscount extends Activity {

	DatabaseHelper objDataBase;
	Calendar myCalendar = Calendar.getInstance();
	StringBuilder strbuilder;
	String Value, DiscountType,SubTotal;
	EditText edtDiscount;
	Button btnSave;  
    double DiscAmount,DiscPercent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.global_discount);
		//getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 520);
		try {
			objDataBase = new DatabaseHelper(this);
			if (getIntent().hasExtra("Value")) {
				Value = getIntent().getStringExtra("Value");
			} 
			if (getIntent().hasExtra("SubTotal")) {
				SubTotal = getIntent().getStringExtra("SubTotal");
			} 
			if (getIntent().hasExtra("DiscountType")) {
				DiscountType = getIntent().getStringExtra("DiscountType");
			}  
			double discValue=0;
			if(DiscountType.equals("P")){
				discValue=Double.valueOf(Value)*100;
				RadioButton btn=  (RadioButton)findViewById(R.id.radio_DiscPer);  
				btn.setChecked(true);
			}else{
				discValue=Double.valueOf(Value);
				RadioButton btn=  (RadioButton)findViewById(R.id.radio_DiscAmt);  
				btn.setChecked(true); 
			} 
			edtDiscount = (EditText) findViewById(R.id.edtDiscount);  
			edtDiscount.setText(String.valueOf(discValue));
			//edtDiscountPer.setText(DiscPer);
			//edtDiscountPer.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "100")});  
			
			/*edtDiscountPer.setOnFocusChangeListener(new OnFocusChangeListener() {    
		        public void onFocusChange(View v, boolean hasFocus) {
		            if(!hasFocus) {
		            	String percent = edtDiscountPer.getText().toString();
						String amt = edtDiscountAmt.getText().toString();
	                    if(isNumeric(percent)){ 
	                   	 try {
	                   		 DiscPercent=Double.valueOf(percent)/100;
	                   		 DiscAmount=Global.RD(Double.valueOf(SubTotal)*(DiscPercent), 2);
	                   		 if(amt.equals(String.valueOf(DiscAmount)))return;
	                   		 edtDiscountAmt.setText(String.valueOf(DiscAmount));  
							} catch (Exception e) { 
								Toast.makeText(GlobalDiscount.this, e.getMessage(), Toast.LENGTH_SHORT) .show();
							}
	                    }else{ 
	                   	 edtDiscountAmt.setText("0.00");
	                    } 
		            }   
		        }
		    });
			
			edtDiscountAmt.setOnFocusChangeListener(new OnFocusChangeListener() {    
		        public void onFocusChange(View v, boolean hasFocus) {
		            if(!hasFocus) {
		            	String amt = edtDiscountAmt.getText().toString();
						String percent = edtDiscountPer.getText().toString();
	                    if(isNumeric(amt)){ 
	                   	 try { 
	                   		 DiscAmount=Double.valueOf(amt);
	                   		 DiscPercent=DiscAmount/Double.valueOf(SubTotal);
	                   		 if(percent.equals(String.valueOf(DiscPercent)))return;
	                   		 edtDiscountPer.setText(String.valueOf(DiscPercent));    
							} catch (Exception e) { 
								Toast.makeText(GlobalDiscount.this, e.getMessage(), Toast.LENGTH_SHORT) .show();
							}
	                    }else{ 
	                   	 edtDiscountPer.setText("0.00");
	                    } 
		            }  

		        }
		    });*/
			 
			btnSave = (Button) findViewById(R.id.btnSave);
			btnSave.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					hideSoftKeyboard(GlobalDiscount.this,v); 
					String percent = edtDiscount.getText().toString(); 
                    if(!isNumeric(percent)){  
                    	percent="0";
                    }
                    String amt = edtDiscount.getText().toString();
                    if(!isNumeric(amt)){ 
                    	amt="0";
                    } 
                    if(DiscountType.equals("P") && Double.valueOf(percent)>100 ){
                    	Toast.makeText(GlobalDiscount.this,"Invalid Discount.", Toast.LENGTH_SHORT) .show();
                    	return;
        			}else if(DiscountType.equals("A") && Double.valueOf(amt)> Double.valueOf(SubTotal) ){
        				Toast.makeText(GlobalDiscount.this,"Invalid Discount.", Toast.LENGTH_SHORT) .show();
                    	return;
        			}  
                    
					Bundle b = new Bundle();
					b.putString("status", "done");
					b.putString("Amt", amt);
					b.putString("Per", String.valueOf(Double.valueOf(percent)/100));
					b.putString("Type", DiscountType);
					Intent intent = new Intent();
					intent.putExtras(b);
					setResult(RESULT_OK, intent);
					finish();
				}
			});

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void onRadioButtonClicked(View view) {
	    // Is the button now checked?
	    boolean checked = ((RadioButton) view).isChecked(); 
	    // Check which radio button was clicked
	    switch(view.getId()) {
	        case R.id.radio_DiscAmt:
	            if (checked) 
	            	DiscountType="A";
	            break;
	        case R.id.radio_DiscPer:
	            if (checked) 
	            	DiscountType="P";
	            break;
	    }
	}

	public static void hideSoftKeyboard (Activity activity, View view) 
	{
	    InputMethodManager imm = (InputMethodManager)activity.getSystemService(activity.INPUT_METHOD_SERVICE);
	    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
	}
	
	public static boolean isNumeric(String str) {
		try {
			double d = Double.parseDouble(str);
		} catch (NumberFormatException nfe) {
			return false;
		}
		return true;
	}

	// CalCulation for Discount from price
	public void funDiscountCalculation() {
		try {

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
